package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.hzdesk.media.koreabatchmatvector.logic.contract.IWifiHotConTract;
import com.hzdesk.media.koreabatchmatvector.utils.HotUtils;

/**
 * Created by Administrator on 2018/8/6.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class WifiHotPersenter implements IWifiHotConTract.IWifiHotPersenter {
    private IWifiHotConTract.IWifiHotView mIWifiHotView;
    public WifiHotPersenter(IWifiHotConTract.IWifiHotView mIWifiHotView) {
        this.mIWifiHotView = mIWifiHotView;
    }

    @Override
    public void onClickedBack() {
        mIWifiHotView.toFinshPage();
    }

    @Override
    public void onClickedOpen(Context mContext) {
        boolean flag =  HotUtils.isWifiApEnabled(mContext);
        Log.e("WifiHotPersenter", "onClickedOpen flag: "+flag );
        String name =  mIWifiHotView.getName();
        String password =   mIWifiHotView.getInputPassword();
        if(!flag){
            mIWifiHotView.showHotSetting();
            mIWifiHotView.showOpen();
          if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)){
              HotUtils.startWifiAp(name,password,mContext,true);
          }
        }else{
            if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)){
                HotUtils.startWifiAp(name,password,mContext,false);
            }
            mIWifiHotView.hideHotSetting();
            mIWifiHotView.showCloase();
        }
    }

    @Override
    public void setCheckBoxListening(AppCompatCheckBox box, Context mContext) {
        if(box.isChecked()){  // 选中
            mIWifiHotView.isOpenEye();
        }else{
            mIWifiHotView.NoOpenEye();
        }
        box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    mIWifiHotView.isOpenEye();
                }else{
                    mIWifiHotView.NoOpenEye();
                }
            }
        });
    }

    @Override
    public void onClickedSave(final Context mContext) {
        onClickedOpen(mContext);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                onClickedOpen(mContext);
            }
        },1000);

    }

    @Override
    public void onClickedCancel(Context mContext) {
        HotUtils.closeWifiAp(mContext);
        mIWifiHotView.hideHotSetting();
        mIWifiHotView.showCloase();
        Toast.makeText(mContext,"取消设置热点信息并关闭热点",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void init(Context mContext) {
        boolean flag =  HotUtils.isWifiApEnabled(mContext);
        if(flag){ //开启热点
            mIWifiHotView.showHotSetting();
            mIWifiHotView.showOpen();
        }else{  //未开启热点
            mIWifiHotView.hideHotSetting();
            mIWifiHotView.showCloase();
        }
    }
}
